﻿package mortal.game.view.task.drama.operations
{
    import Message.DB.Tables.*;
    import __AS3__.vec.*;
    import baseEngine.modifiers.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import flash.geom.*;
    import flash.text.*;
    import frEngine.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import frEngine.render.layer.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.game.view.task.data.*;
    import mortal.game.view.task.drama.*;
    import mortal.game.view.task.drama.operations.npctalk.*;

    public class TaskDramaOpTalk extends Window implements ITaskDramaStepCommand
    {
        private var _config:TTaskDrama;
        private var _text:TaskDramaTalkMaskText;
        private var _txtName:GTextFiled;
        private var _isShowing:Boolean;
        private var _isShowingSelf:Boolean = false;
        private var _lastIconName:String;
        private const BgHeight:int = 241;
        private const BgWidth:int = 1179;
        private var _mouse:GBitmap;
        private var _img2d:Img2D;
        private var _img2d2:Img2D;
        private var _roleModel:RoleModelPlayer;
        private var _bodyPlayer:ActionPlayer;
        private var _npc3d:Rect3DObject;
        private var _hangPlayer:ActionPlayer;
        protected var _effectPlayerArray:Vector.<EffectPlayer>;
        private var _callback:Function;
        private var _tween1:TweenMax;
        private var _tween2:TweenMax;
        private var _tween3:TweenMax;
        private var _lastShowLabel:int = -11111111;

        public function TaskDramaOpTalk()
        {
            this._effectPlayerArray = new Vector.<EffectPlayer>;
            this._isShowing = false;
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        public function call(param1:TTaskDrama, param2:Function = null) : void
        {
            if (isHide)
            {
                this.show();
            }
            this._callback = param2;
            this._config = param1;
            if (this._isShowing)
            {
                this.stopCall();
            }
            this.addTalkText();
            this._isShowing = true;
            this._mouse.visible = true;
            this.updateToCurNpc();
            return;
        }// end function

        public function cancel(param1:TTaskDrama, param2:Function = null) : void
        {
            var _loc_3:Object = null;
            Log.error("剧情，TaskDramaOpTalk，执行cancel开始！");
            this.killAllTween();
            DisplayUtil.removeMe(this._mouse);
            this._callback = param2;
            this.stopCall();
            this._txtName.text = "";
            var _loc_4:int = 450;
            if (!this._isShowingSelf)
            {
                _loc_3 = this._npc3d.getMesh3dInfo(this._bodyPlayer);
            }
            else
            {
                _loc_3 = this._npc3d.getMesh3dInfo(this._roleModel.bodyPlayer);
                _loc_4 = -450;
            }
            if (_loc_3 != null)
            {
                this._tween1 = TweenMax.to(_loc_3, 0.25, {offsetX:_loc_4, alpha:0.3, ease:Quint.easeOut});
            }
            this._tween2 = TweenMax.to(this._img2d, 0.4, {y:this.BgHeight + 5});
            this._tween3 = TweenMax.to(this._img2d2, 0.4, {y:-Global.stage.stageHeight - 5, onComplete:this.cancelEnd});
            this._lastShowLabel = -11111111;
            return;
        }// end function

        private function cancelEnd() : void
        {
            Log.error("剧情，TaskDramaOpTalk，执行cancel结束了！");
            this._tween1 = null;
            this._tween2 = null;
            this._tween3 = null;
            if (this._callback != null)
            {
                this._callback.apply();
            }
            this.hide();
            return;
        }// end function

        public function showBlackBgs(param1:Function) : void
        {
            super.show();
            this._callback = param1;
            var _loc_2:* = UIFactory.bg(0, 0, Global.stage.stageWidth, 241, null, ImagesConst.TaskDramaDownBg);
            this._img2d = new Img2D(null, _loc_2.bitmapData, new Rectangle(0, 0, _loc_2.bitmapData.width, _loc_2.bitmapData.height), true);
            _loc_2.dispose(true);
            _loc_2 = UIFactory.bg(0, 0, Global.stage.stageWidth, 153, null, ImagesConst.TaskDramaUpBg);
            this._img2d2 = new Img2D(null, _loc_2.bitmapData, new Rectangle(0, 0, _loc_2.bitmapData.width, _loc_2.bitmapData.height), true);
            _loc_2.dispose(true);
            this._npc3d = Rect3DManager.instance.getRect3dByWindow(this);
            if (!this._npc3d)
            {
                this._npc3d = Rect3DManager.instance.registerWindow(false, new Rectangle(0, 0, this._img2d.width, this._img2d.height), this, true);
            }
            Rect3DManager.instance.windowShowHander(null, this);
            this._npc3d.addImg(this._img2d, Layer3DManager.MapLayer);
            this._npc3d.addImg(this._img2d2, Layer3DManager.MapLayer);
            this.onStageResize();
            this.killAllTween();
            this._img2d.y = this.BgHeight + 5;
            this._tween1 = TweenMax.to(this._img2d, 0.4, {y:0});
            this._img2d2.y = -Global.stage.stageHeight - 5;
            this._tween2 = TweenMax.to(this._img2d2, 0.4, {y:-Global.stage.stageHeight + this.BgHeight, onComplete:this.showBlackEnd});
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.onStageResize();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            if (_closeBtn != null)
            {
                DisplayUtil.removeMe(_closeBtn);
                _closeBtn = null;
            }
            this._txtName = UIFactory.gTextField("", 500, 114, 200, 25, this);
            this._txtName.textColor = 65066;
            var _loc_1:* = this._txtName.defaultTextFormat;
            _loc_1.size = 18;
            this._txtName.defaultTextFormat = _loc_1;
            this.addTalkText();
            this._mouse = UIFactory.gBitmap(null, 0, 0, this);
            this._mouse.visible = false;
            LoaderHelp.addResCallBack(ResFileConst.dramaTalk, this.resGotHandler);
            return;
        }// end function

        private function addTalkText() : void
        {
            if (this._text == null)
            {
                this._text = UICompomentPool.getUICompoment(TaskDramaTalkMaskText) as TaskDramaTalkMaskText;
                this._text.x = 230;
                this._text.y = this._txtName.y + 30;
                this.addChild(this._text);
            }
            return;
        }// end function

        private function showBlackEnd() : void
        {
            this._tween1 = null;
            this._tween2 = null;
            if (this._callback != null)
            {
                this._callback.apply();
            }
            return;
        }// end function

        private function resGotHandler() : void
        {
            if (isDisposed)
            {
                return;
            }
            this._mouse.bitmapData = GlobalClass.getBitmapData(ImagesConst.Drama_Mouse);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (this._npc3d)
            {
                if (this._bodyPlayer != null)
                {
                    this._npc3d.disposeObj3d(this._bodyPlayer);
                    if (this._hangPlayer != null)
                    {
                        this._bodyPlayer.unHang(this._hangPlayer);
                    }
                    this._bodyPlayer = null;
                }
                if (this._roleModel != null)
                {
                    this._npc3d.disposeObj3d(this._roleModel.bodyPlayer);
                }
                if (this._img2d != null)
                {
                    this._npc3d.removeImg(this._img2d);
                }
                if (this._img2d2 != null)
                {
                    this._npc3d.disposeObj3d(this._img2d2);
                }
                Rect3DManager.instance.disposeRect3d(this._npc3d);
                this._npc3d = null;
            }
            this._effectPlayerArray.length = 0;
            if (this._hangPlayer)
            {
                if (this._bodyPlayer != null)
                {
                    this._bodyPlayer.unHang(this._hangPlayer);
                    this._bodyPlayer.dispose();
                    this._bodyPlayer = null;
                }
                this._hangPlayer.dispose();
                this._hangPlayer = null;
            }
            if (this._bodyPlayer != null)
            {
                this._bodyPlayer.dispose();
                this._bodyPlayer = null;
            }
            if (this._roleModel != null)
            {
                this._roleModel.dispose();
            }
            if (this._img2d != null)
            {
                this._img2d.parent = null;
                this._img2d = null;
            }
            if (this._img2d2 != null)
            {
                this._img2d2.parent = null;
                this._img2d2 = null;
            }
            if (this._mouse != null)
            {
                this._mouse.dispose(param1);
                this._mouse = null;
            }
            this._callback = null;
            this._txtName.text = "";
            this._lastShowLabel = -11111111;
            return;
        }// end function

        private function killAllTween() : void
        {
            if (this._tween1 != null)
            {
                this._tween1.kill();
                this._tween1 = null;
            }
            if (this._tween2 != null)
            {
                this._tween2.kill();
                this._tween2 = null;
            }
            if (this._tween3 != null)
            {
                this._tween3.kill();
                this._tween3 = null;
            }
            return;
        }// end function

        public function onStageResize() : void
        {
            if (this.parent == null)
            {
                return;
            }
            if (this._text != null)
            {
                this._text.resize(512);
            }
            this.y = Global.stage.stageHeight - this.BgHeight;
            this.x = 0;
            if (this._img2d2 != null)
            {
                this._img2d2.y = -this.y;
            }
            return;
        }// end function

        public function stopCall() : void
        {
            if (this._text != null)
            {
                this._text.dispose(true);
                this._text = null;
            }
            if (this._txtName != null)
            {
                this._txtName.text = "";
            }
            this._isShowing = false;
            return;
        }// end function

        private function getNpcTalkData(param1:TTaskDrama) : TaskDramaTalkData
        {
            var _loc_2:* = new TaskDramaTalkData();
            _loc_2.popupTime = 0;
            _loc_2.rowWidth = 512;
            _loc_2.speed = 10;
            _loc_2.talk = GLinkTextDataParser.parseToHtmlFormat(param1.talkText);
            if (_loc_2.talk == null)
            {
                _loc_2.talk = "策划没配置剧情的对话";
            }
            if (TaskDramaOperator.ationTime != 0)
            {
                _loc_2.showTime = TaskDramaOperator.ationTime * 1000;
            }
            else
            {
                _loc_2.showTime = 100;
            }
            _loc_2.talkFontLeading = 4;
            _loc_2.talkFontSize = 15;
            return _loc_2;
        }// end function

        private function onShowEnd() : void
        {
            Log.error("..........xxxx........OpTalk， 滚动文字结束了");
            this.stopCall();
            this._isShowing = false;
            if (this._callback != null)
            {
                this._callback.apply();
                this._callback = null;
            }
            return;
        }// end function

        protected function updateToCurNpc() : void
        {
            var _loc_2:Obj3dInfo = null;
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:Array = null;
            var _loc_8:EffectPlayer = null;
            if (this._npc3d == null || this._config == null)
            {
                return;
            }
            this._txtName.text = "";
            var _loc_1:* = NPCConfig.instance.getInfoByCode(this._config.talkNpc);
            this.killAllTween();
            if (_loc_1 != null)
            {
                this._isShowingSelf = false;
                this._txtName.textColor = 65066;
                this._txtName.x = 500;
                this._text.x = 500;
                this._mouse.x = 300;
                this._mouse.y = 120;
                if (this._roleModel)
                {
                    this._npc3d.disposeObj3d(this._roleModel.bodyPlayer);
                }
                if (this._config.talkNpc == this._lastShowLabel)
                {
                    this.bodyTweenEnd();
                    return;
                }
                this._lastShowLabel = this._config.talkNpc;
                if (this._bodyPlayer != null)
                {
                    this._bodyPlayer.dispose();
                }
                this._bodyPlayer = FrObjectPool.getObject(ActionPlayer, this);
                this._bodyPlayer.load(_loc_1.mesh, _loc_1.bone, _loc_1.texture, this._npc3d.renderList);
                this._bodyPlayer.play();
                this._bodyPlayer.scaleValue = 3;
                this._bodyPlayer.changeAction(ActionName.Stand);
                _loc_2 = this._npc3d.addObject3d(this._bodyPlayer, Global.stage.stageWidth - 300, 235);
                _loc_2.offsetX = 300;
                this._tween1 = TweenMax.to(_loc_2, 0.4, {offsetX:0, onComplete:this.bodyTweenEnd});
                if (_loc_1.hangModel)
                {
                    _loc_3 = _loc_1.hangModel.split("#");
                    if (_loc_3.length >= 4)
                    {
                        if (!this._hangPlayer)
                        {
                            this._hangPlayer = FrObjectPool.getObject(ActionPlayer, this);
                            this._hangPlayer.changeAction(ActionName.Stand);
                            this._hangPlayer.selectEnabled = true;
                            this._hangPlayer.timerContorler = TimeControler.defaultTimeControler;
                            this._hangPlayer.play();
                        }
                        this._hangPlayer.load(_loc_3[0], _loc_3[1], _loc_3[2], this._npc3d.renderList);
                        this._hangPlayer.hangBoneName = _loc_3[3];
                        this._bodyPlayer.hang(this._hangPlayer);
                    }
                }
                if (_loc_1.selfEffect)
                {
                    _loc_4 = [];
                    _loc_5 = [];
                    _loc_3 = _loc_1.selfEffect.split("&");
                    _loc_6 = 0;
                    while (_loc_6 < _loc_3.length)
                    {
                        
                        _loc_7 = String(_loc_3[_loc_6]).split("#");
                        _loc_4.push(_loc_7[0]);
                        _loc_5.push(_loc_7[1]);
                        _loc_6++;
                    }
                    _loc_6 = 0;
                    while (_loc_6 < _loc_4.length)
                    {
                        
                        if (this._effectPlayerArray.length > _loc_6)
                        {
                            _loc_8 = this._effectPlayerArray[_loc_6];
                            this._bodyPlayer.unHang(_loc_8);
                            _loc_8.dispose();
                        }
                        _loc_8 = EffectPlayerPool.instance.getEffectPlayer(_loc_4[_loc_6], this._bodyPlayer, null, true);
                        this._effectPlayerArray[_loc_6] = _loc_8;
                        _loc_8.hangBoneName = _loc_5[_loc_6];
                        this._bodyPlayer.hang(_loc_8);
                        _loc_8.play(PlayMode.ANIMATION_LOOP_MODE);
                        _loc_6++;
                    }
                }
            }
            else
            {
                this._isShowingSelf = true;
                this._txtName.textColor = 16776960;
                this._txtName.x = 330;
                this._text.x = 330;
                this._mouse.x = 900;
                this._mouse.y = 120;
                if (this._bodyPlayer != null)
                {
                    this._npc3d.disposeObj3d(this._bodyPlayer);
                    this._bodyPlayer = null;
                }
                if (this._lastShowLabel == -1)
                {
                    this.bodyTweenEnd();
                    return;
                }
                this._lastShowLabel = -1;
                if (this._roleModel != null)
                {
                    this._roleModel.dispose();
                }
                this._roleModel = FrObjectPool.getObject(RoleModelPlayer, this);
                this._roleModel.entityInfo = Cache.instance.role.roleEntityInfo;
                this._roleModel.scaleAll = 3;
                this._roleModel.setRenderList(this._npc3d.renderList);
                _loc_2 = this._npc3d.addObject3d(this._roleModel.bodyPlayer, 220, 215);
                _loc_2.offsetX = -300;
                this._tween1 = TweenMax.to(_loc_2, 0.4, {offsetX:0, onComplete:this.bodyTweenEnd});
            }
            return;
        }// end function

        private function bodyTweenEnd() : void
        {
            Log.error("..........xxxx........OpTalk， bodyTweenEnd");
            this._tween1 = null;
            if (this._text == null || this._config == null)
            {
                this.onShowEnd();
                return;
            }
            var _loc_1:* = NPCConfig.instance.getInfoByCode(this._config.talkNpc);
            if (_loc_1 != null)
            {
                this._txtName.htmlText = _loc_1.name;
            }
            else
            {
                this._txtName.htmlText = Cache.instance.role.playerInfo.name + ":";
            }
            Log.error("..........xxxx........OpTalk， 开始显示说话的文字");
            this._text.show(this.getNpcTalkData(this._config), this.onShowEnd);
            return;
        }// end function

    }
}
